package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.tree.AbstractLayoutCache;
	
	public class FrameCreate extends JFrame
	{
		//members define
		private var panel46:JPanel;
		private var panel_image:JPanel;
		private var panel_description:JPanel;
		private var txt_description:JTextArea;
		private var panel49:JPanel;
		private var panel29:JPanel;
		private var panel10:JPanel;
		private var btn_human_boy:JToggleButton;
		private var panel_race_human:JPanel;
		private var btn_human_girl:JToggleButton;
		private var panel11:JPanel;
		private var btn_devil_boy:JToggleButton;
		private var panel_race_devil:JPanel;
		private var btn_devil_girl:JToggleButton;
		private var panel12:JPanel;
		private var btn_immortal_boy:JToggleButton;
		private var panel_race_immortal:JPanel;
		private var btn_immortal_girl:JToggleButton;
		private var panel30:JPanel;
		private var panel24:JPanel;
		private var panel_input_name:JPanel;
		private var _txtName:JTextField;
		private var _labelMessage:JLabel;
		private var panel31:JPanel;
		private var _btn_create:JButton;
		
		private var _desc_human:String = "人族,技能:混乱,撕裂,强身.";
		private var _desc_devil:String = "魔族,技能:冰冻,降灾,嘲讽.";
		private var _desc_immortal:String = "仙族,技能:昏睡,闪电,驱散.";
		public function FrameCreate()
		{
			setClosable(false);
			setResizable(false);  
			setBorder(new EmptyBorder());
			this.getTitleBar().setMinimizeHeight(0);
			
			//component creation
			setSize(new IntDimension(557, 486));
			setPreferredSize(new IntDimension(557, 486));
			var layout0:SoftBoxLayout = new SoftBoxLayout();
			layout0.setAxis(AsWingConstants.HORIZONTAL);
			layout0.setAlign(AsWingConstants.LEFT);
			layout0.setGap(0);
			getContentPane().setLayout(layout0);
			
			panel46 = new JPanel();
			panel46.setLocation(new IntPoint(0, 13));
			panel46.setSize(new IntDimension(367, 535));
			panel46.setPreferredSize(new IntDimension(367, 535));
			var layout1:EmptyLayout = new EmptyLayout();
			panel46.setLayout(layout1);
			
			panel_image = new JPanel();
			panel_image.setLocation(new IntPoint(0, 0));
			panel_image.setSize(new IntDimension(367, 346));
			panel_image.setPreferredSize(new IntDimension(367, 346));
			
			panel_description = new JPanel();
			panel_description.setLocation(new IntPoint(19, 346));
			panel_description.setSize(new IntDimension(329, 135));
			panel_description.setPreferredSize(new IntDimension(329, 135));
			var layout2:EmptyLayout = new EmptyLayout();
			panel_description.setLayout(layout2);
			
			txt_description = new JTextArea();
			txt_description.setLocation(new IntPoint(8, 24));
			txt_description.setSize(new IntDimension(313, 103));
			txt_description.setPreferredSize(new IntDimension(313, 103));
			
			panel49 = new JPanel();
			panel49.setLocation(new IntPoint(0, 257));
			panel49.setSize(new IntDimension(190, 535));
			panel49.setPreferredSize(new IntDimension(190, 535));
			var layout3:SoftBoxLayout = new SoftBoxLayout();
			layout3.setAxis(AsWingConstants.VERTICAL);
			layout3.setAlign(AsWingConstants.TOP);
			layout3.setGap(3);
			panel49.setLayout(layout3);
			
			panel29 = new JPanel();
			panel29.setLocation(new IntPoint(0, 0));
			panel29.setSize(new IntDimension(190, 50));
			panel29.setPreferredSize(new IntDimension(190, 50));
			
			panel10 = new JPanel();
			panel10.setLocation(new IntPoint(0, 0));
			panel10.setSize(new IntDimension(190, 78));
			panel10.setPreferredSize(new IntDimension(190, 78));
			var layout4:FlowLayout = new FlowLayout();
			layout4.setAlignment(AsWingConstants.LEFT);
			layout4.setHgap(0);
			layout4.setVgap(0);
			panel10.setLayout(layout4);
			
			btn_human_boy = new JToggleButton();
			btn_human_boy.setLocation(new IntPoint(1, 0));
			btn_human_boy.setSize(new IntDimension(78, 78));
			btn_human_boy.setPreferredSize(new IntDimension(78, 78));
			btn_human_boy.setText("label");
			
			panel_race_human = new JPanel();
			panel_race_human.setLocation(new IntPoint(78, 5));
			panel_race_human.setSize(new IntDimension(34, 67));
			panel_race_human.setPreferredSize(new IntDimension(34, 67));
			
			btn_human_girl = new JToggleButton();
			btn_human_girl.setLocation(new IntPoint(56, 78));
			btn_human_girl.setSize(new IntDimension(78, 78));
			btn_human_girl.setPreferredSize(new IntDimension(78, 78));
			btn_human_girl.setText("label");
			
			panel11 = new JPanel();
			panel11.setLocation(new IntPoint(0, 10));
			panel11.setSize(new IntDimension(190, 78));
			panel11.setPreferredSize(new IntDimension(190, 78));
			var layout5:FlowLayout = new FlowLayout();
			layout5.setAlignment(AsWingConstants.LEFT);
			layout5.setHgap(0);
			layout5.setVgap(0);
			panel11.setLayout(layout5);
			
			btn_devil_boy = new JToggleButton();
			btn_devil_boy.setLocation(new IntPoint(5, 5));
			btn_devil_boy.setSize(new IntDimension(31, 22));
			btn_devil_boy.setPreferredSize(new IntDimension(78, 78));
			btn_devil_boy.setText("label");
			
			panel_race_devil = new JPanel();
			panel_race_devil.setLocation(new IntPoint(88, 10));
			panel_race_devil.setSize(new IntDimension(34, 67));
			panel_race_devil.setPreferredSize(new IntDimension(34, 67));
			
			btn_devil_girl = new JToggleButton();
			btn_devil_girl.setLocation(new IntPoint(103, 33));
			btn_devil_girl.setSize(new IntDimension(31, 22));
			btn_devil_girl.setPreferredSize(new IntDimension(78, 78));
			btn_devil_girl.setText("label");
			
			panel12 = new JPanel();
			panel12.setLocation(new IntPoint(0, 20));
			panel12.setSize(new IntDimension(190, 78));
			panel12.setPreferredSize(new IntDimension(190, 78));
			var layout6:FlowLayout = new FlowLayout();
			layout6.setAlignment(AsWingConstants.LEFT);
			layout6.setHgap(0);
			layout6.setVgap(0);
			panel12.setLayout(layout6);
			
			btn_immortal_boy = new JToggleButton();
			btn_immortal_boy.setLocation(new IntPoint(5, 5));
			btn_immortal_boy.setSize(new IntDimension(31, 22));
			btn_immortal_boy.setPreferredSize(new IntDimension(78, 78));
			btn_immortal_boy.setText("label");
			
			panel_race_immortal = new JPanel();
			panel_race_immortal.setLocation(new IntPoint(41, 11));
			panel_race_immortal.setSize(new IntDimension(10, 10));
			panel_race_immortal.setPreferredSize(new IntDimension(34, 67));
			
			btn_immortal_girl = new JToggleButton();
			btn_immortal_girl.setLocation(new IntPoint(103, 33));
			btn_immortal_girl.setSize(new IntDimension(31, 22));
			btn_immortal_girl.setPreferredSize(new IntDimension(78, 78));
			btn_immortal_girl.setText("label");
			
			panel30 = new JPanel();
			panel30.setLocation(new IntPoint(0, 296));
			panel30.setSize(new IntDimension(190, 65));
			panel30.setPreferredSize(new IntDimension(190, 65));
			
			panel24 = new JPanel();
			panel24.setLocation(new IntPoint(0, 234));
			panel24.setSize(new IntDimension(190, 48));
			panel24.setPreferredSize(new IntDimension(142, 48));
			var layout7:CenterLayout = new CenterLayout();
			panel24.setLayout(layout7);
			
			panel_input_name = new JPanel();
			panel_input_name.setLocation(new IntPoint(5, 5));
			panel_input_name.setSize(new IntDimension(10, 10));
			panel_input_name.setPreferredSize(new IntDimension(142, 48));
			var layout8:EmptyLayout = new EmptyLayout();
			panel_input_name.setLayout(layout8);
			
			_txtName = new JTextField();
			_txtName.setLocation(new IntPoint(3, 24));
			_txtName.setSize(new IntDimension(137, 21));
			_txtName.setPreferredSize(new IntDimension(137, 21));
			
			_labelMessage = new JLabel();
			_labelMessage.setLocation(new IntPoint(0, 17));
			_labelMessage.setSize(new IntDimension(190, 25));
			_labelMessage.setPreferredSize(new IntDimension(409, 25));
			_labelMessage.setText("label");
			
			panel31 = new JPanel();
			panel31.setLocation(new IntPoint(0, 451));
			panel31.setSize(new IntDimension(190, 29));
			var layout9:CenterLayout = new CenterLayout();
			panel31.setLayout(layout9);
			
			_btn_create = new JButton();
			_btn_create.setLocation(new IntPoint(180, 0));
			_btn_create.setSize(new IntDimension(49, 36));
			_btn_create.setPreferredSize(new IntDimension(76, 36));
			_btn_create.setText("label");
			
			//component layoution
			getContentPane().append(panel46);
			getContentPane().append(panel49);
			
			panel46.append(panel_image);
			panel46.append(panel_description);
			
			panel_description.append(txt_description);
			
			panel49.append(panel29);
			panel49.append(panel10);
			panel49.append(panel11);
			panel49.append(panel12);
			panel49.append(panel30);
			panel49.append(panel24);
			panel49.append(_labelMessage);
			panel49.append(panel31);
			
			panel10.append(btn_human_boy);
			panel10.append(panel_race_human);
			panel10.append(btn_human_girl);
			
			panel11.append(btn_devil_boy);
			panel11.append(panel_race_devil);
			panel11.append(btn_devil_girl);
			
			panel12.append(btn_immortal_boy);
			panel12.append(panel_race_immortal);
			panel12.append(btn_immortal_girl);
			
			panel24.append(panel_input_name);
			
			panel_input_name.append(_txtName);
			
			panel31.append(_btn_create);
			
			
			//-----------------------------------------------------
			
			//_btn_boy.addActionListener(onBoy);
			//_btn_girl.addActionListener(onGirl);
			//_btn_boy.wrapSimpleButton(Client.getUIElement("btn_boy") as SimpleButton);
			//_btn_girl.wrapSimpleButton(Client.getUIElement("btn_girl") as SimpleButton);


			setImages();
		}
		
		public function setImages():void
		{
			//setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_chat_bg") as DisplayObject));
			
			txt_description.setForeground(ASColor.WHITE);
			txt_description.setEditable(false);
			txt_description.setWordWrap(true);
			txt_description.scrollToBottomLeft();
			txt_description.setBackgroundDecorator(null);
			
			var filter:GlowFilter;
			filter = new GlowFilter(0x000000, 1, 2, 2, 1000);
			
			_txtName.setMaxChars(12);
			_txtName.setFont(new ASFont("宋体", 12, false, false, false, false));
			_txtName.setForeground(null);
			_txtName.setBackground(null);
			_txtName.setBorder(null);
			_txtName.addActionListener(onCreate);
			_txtName.setForeground( ASColor.WHITE );
			_txtName.setBackground( ASColor.WHITE );
			_txtName.filters = new Array(filter);
			_txtName.setBackgroundDecorator(null);
			
			_labelMessage.setFont(new ASFont("宋体", 14, false, false, false, false));
			filter = new GlowFilter(0x000000, 1, 2, 2, 10);
			_labelMessage.setTextFilters(new Array(filter));
			_labelMessage.setForeground( ASColor.RED ); 
			_labelMessage.setText("");
			
			
			_btn_create.addActionListener(onCreate);
			_btn_create.wrapSimpleButton(Client.getUIElement("btn_create") as SimpleButton);
			
			_btn_create.setText("");
			
			//var img_create:DisplayObject = Client.getUIElement("img_create") as DisplayObject
			//setBackgroundDecorator(new AssetBackground(img_create));
			setBackgroundDecorator(null);
			
			//setSizeWH(img_create.width,img_create.height);
			setLocationXY((Engine.W - width) / 2,(Engine.H - height)/2);
			
			/*if(Engine.nickname.length != 0)
			{
			_btn_boy.setSelected(Engine.m_nGender == 0);
			_btn_girl.setSelected(Engine.m_nGender == 1);
			_txtName.setText(Engine.nickname);
			_btn_boy.setEnabled(false);
			_btn_girl.setEnabled(false);
			onCreate(null);
			}*/
			//addEventListener(FrameEvent.FRAME_CLOSING,onFrameClosing);
			
			btn_human_boy.wrapSimpleButton(Client.getUIElement("btn_human_boy") as SimpleButton);
			btn_human_boy.setText("");
			btn_human_girl.wrapSimpleButton(Client.getUIElement("btn_human_girl") as SimpleButton);
			btn_human_girl.setText("");
			btn_devil_boy.wrapSimpleButton(Client.getUIElement("btn_devil_boy") as SimpleButton);
			btn_devil_boy.setText("");
			btn_devil_girl.wrapSimpleButton(Client.getUIElement("btn_devil_girl") as SimpleButton);
			btn_devil_girl.setText("");
			btn_immortal_boy.wrapSimpleButton(Client.getUIElement("btn_immortal_boy") as SimpleButton);
			btn_immortal_boy.setText("");
			btn_immortal_girl.wrapSimpleButton(Client.getUIElement("btn_immortal_girl") as SimpleButton);
			btn_immortal_girl.setText("");
			var group:ButtonGroup = new ButtonGroup();
			group.appendAll(btn_human_boy,btn_human_girl,btn_devil_boy,btn_devil_girl,btn_immortal_boy,btn_immortal_girl);
			
			panel_description.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_race_description") as DisplayObject));
			panel_input_name.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_create_input_name") as DisplayObject));
			
			panel_race_human.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_human") as DisplayObject));
			panel_race_devil.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_devil") as DisplayObject));
			panel_race_immortal.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_immortal") as DisplayObject));
			
			btn_human_boy.addActionListener(onBtnHumanBoy);
			btn_human_girl.addActionListener(onBtnHumanGirl);
			btn_devil_boy.addActionListener(onBtnDevilBoy);
			btn_devil_girl.addActionListener(onBtnDevilGirl);
			btn_immortal_boy.addActionListener(onBtnImmortalBoy);
			btn_immortal_girl.addActionListener(onBtnImmortalGirl);
			
			panel_description.setVisible(false);
			
		}
		private var _race:int = 0;
		private var _gender:int = 0;
		protected function onBtnHumanBoy(e:Event):void
		{
			panel_image.setBackgroundDecorator(null);
			panel_image.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_human_boy") as DisplayObject));
			panel_description.setVisible(true);
			txt_description.setHtmlText(_desc_human);
			_race = 0;
			_gender = 0;
		}
		protected function onBtnHumanGirl(e:Event):void
		{
			panel_image.setBackgroundDecorator(null);
			panel_image.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_human_girl") as DisplayObject));
			panel_description.setVisible(true);
			txt_description.setHtmlText(_desc_human);
			_race = 0;
			_gender = 1;
		}
		protected function onBtnDevilBoy(e:Event):void
		{
			panel_image.setBackgroundDecorator(null);
			panel_image.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_devil_boy") as DisplayObject));
			panel_description.setVisible(true);
			txt_description.setHtmlText(_desc_devil);
			_race = 1;
			_gender = 0;
		}
		protected function onBtnDevilGirl(e:Event):void
		{
			panel_image.setBackgroundDecorator(null);
			panel_image.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_devil_girl") as DisplayObject));
			panel_description.setVisible(true);
			txt_description.setHtmlText(_desc_devil);
			_race = 1;
			_gender = 1;
		}
		protected function onBtnImmortalBoy(e:Event):void
		{
			panel_image.setBackgroundDecorator(null);
			panel_image.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_immortal_boy") as DisplayObject));
			panel_description.setVisible(true);
			txt_description.setHtmlText(_desc_immortal);
			_race = 2;
			_gender = 0;
		}
		protected function onBtnImmortalGirl(e:Event):void
		{
			panel_image.setBackgroundDecorator(null);
			panel_image.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_immortal_girl") as DisplayObject));
			panel_description.setVisible(true);
			txt_description.setHtmlText(_desc_immortal);
			_race = 2;
			_gender = 1;
		}
		private function onFrameClosing(e:Event):void
		{
			_btn_create.wrapSimpleButton(new SimpleButton);
			setBackgroundDecorator(null);
		}
		protected function onCreate(e:Event):void
		{
			if(!btn_human_boy.isSelected() && !btn_human_girl.isSelected()
				&& !btn_devil_boy.isSelected()&& !btn_devil_girl.isSelected()
				&& !btn_immortal_boy.isSelected()&& !btn_immortal_girl.isSelected())
			{
				_labelMessage.setText("请选择种族!");
				validate();
				return;
			}
			if(_txtName.getText().length <= 0)
			{
				_labelMessage.setText("请输入角色名!");
				validate();
				return;
			}
			if(_txtName.getText().length < 2)
			{
				_labelMessage.setText("角色名过短!");
				validate();
				return;
			}
			if(_txtName.getText().length >= 16)
			{
				_labelMessage.setText("角色名过长!");
				validate();
				return;
			}
			if(int(_txtName.getText()) != 0)
			{
				_labelMessage.setText("角色名字必须包含字母!");
				validate();
				return;
			}
			//JOptionPane.showMessageDialog("登陆","登陆,这里发送消息给服务器!");
			trace("CharInfo.accountId:",CharInfo.accountId)
			//Client.getInstance().sendPacketLogic(4,[CharInfo.accountId,_btn_boy.isSelected() ? 0 : 1, _txtName.getText()]);
			Client.getInstance().sendPacketLogic(4,[CharInfo.accountId,_race,_gender, _txtName.getText()]);
			Client.getInstance().addEventListener("4", phCreateCharacter_4);
			
			_txtName.setEnabled(false);
			_btn_create.setEnabled(false);
			
			//_panelMain.openMap();
			//closeReleased();
		}
		
		private function phCreateCharacter_4(e:PacketEvent):void
		{
			Client.getInstance().removeEventListener("4", phCreateCharacter_4);
			
			var msg:Array = e.data as Array;
			trace("phCreateCharacter_4", msg);
			switch(msg[0])
			{
				case 0:
				{
					showMessage("创建角色成功,正在加载玩家信息...");
					//_panelMain.createCharacter();
					//_panelMain.openMap();
					//closeReleased();
					return;
					//break;
				}
				case 1:
				{
					showMessage("角色名已存在!");
					break;
				}
				case 2:
				{
					showMessage("此帐号已经创建过角色!");
					break;
				}
				case 3:
				{
					showMessage("数据库操作出错!");
					break;
				}
				case 4:
				{
					showMessage("角色名过长!");
					break;
				}
			}
			_txtName.setEnabled(true);
			_btn_create.setEnabled(true);
		}
		
		private function showMessage(msg:String):void
		{
			_labelMessage.setText(msg);
			validate();
		}
		
		public static var _frame:FrameCreate = null;
		public static function show():void
		{
			FrameLogin.hide();
			FrameRegister.hide();
			if(isShowing())
			{
				return;
			}
			if(_frame == null)
			{
				_frame = new FrameCreate();
				PanelMain.getInstance().addChild(_frame);
			}
			_frame.show();
			PanelMain.getInstance().setBackgroundDecorator(null);
			PanelMain.getInstance().setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_create_background") as DisplayObject));
			PanelMain.getInstance().showPanelMainHelp(false);
			PanelMain.getInstance().bringToTop(_frame);
			
		}
		public static function hide():void
		{
			if(isShowing())_frame.hide();
		}
		public static function isShowing():Boolean
		{
			return(_frame != null&& _frame.isShowing());
		}
	}
}